package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.community.dto.ArticleCreateRequest;
import com.youlu.campus.admin.community.dto.ArticleQueryRequest;
import com.youlu.campus.admin.community.dto.ArticleUpdateRequest;
import com.youlu.campus.admin.community.dto.WeightRequest;
import com.youlu.campus.admin.community.service.ArticleService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.base.base.GeneraStatusEnum;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 文章管理控制层
 *
 * @author: YangJ
 * @date: 2020/11/24 18:05
 */
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 创建文章
     *
     * @param request
     * @return
     */
    @PostMapping("/article/create")
    public QResult createArticle(@RequestBody @NotNull ArticleCreateRequest request) {
        request.setUserId(AuthUtils.getUserId());
        boolean result = articleService.createArticle(request);
        return QResult.success(result);
    }

    /**
     * 修改文章
     *
     * @param request 文章信息修改请求类
     * @return
     */
    @PostMapping("/article/update")
    public QResult updateArticle(@RequestBody ArticleUpdateRequest request) {
        boolean result = articleService.updateArticle(request);
        return QResult.success(result);
    }

    /**
     * 上架
     *
     * @param id 文章ID
     * @return
     */
    @PostMapping("/article/on")
    public QResult onArticle(String id) {
        boolean result = articleService.updateArticleStatus(id, GeneraStatusEnum.ON.getCode());
        return QResult.success(result);
    }

    /**
     * 下架
     *
     * @param id 文章ID
     * @return
     */
    @PostMapping("/article/off")
    public QResult offArticle(String id) {
        boolean result = articleService.updateArticleStatus(id, GeneraStatusEnum.OFF.getCode());
        return QResult.success(result);
    }

    /**
     * 修改文章权重
     *
     * @param request 权重信息请求类
     * @return
     */
    @PostMapping("/article/update/weight")
    public QResult updateWeight(@RequestBody WeightRequest request) {
        boolean result = articleService.updateWeight(request);
        return QResult.success(result);
    }

    /**
     * 删除文章
     *
     * @param id 文章ID
     * @return
     */
    @DeleteMapping("/article/delete")
    public QResult deleteArticle(String id) {
        boolean result = articleService.deleteArticle(id);
        return QResult.success(result);
    }

    /**
     * 文章列表
     *
     * @param request 文章信息查询请求类
     * @return
     */
    @PostMapping("/article/list")
    public QResult articleRoomList(@RequestBody ArticleQueryRequest request) {
        return QResult.success(articleService.articleList(request));
    }

    /**
     * 文章信息查询
     *
     * @param id 文章ID
     * @return
     */
    @GetMapping("/article/query")
    public QResult articleRoomList(String id) {
        return QResult.success(articleService.query(id));
    }
}
